package com.xinfeng.commons.lang;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class QueueUtils {

	public static void bubbleSort(List list, Comparator comparator){
		int size = list.size();
		
		for(int i = 0; i < size; i++){
			Object obj1 = list.get(i);
			
			for(int j = i + 1; j < size; j++){
				Object obj2 = list.get(j);
				
				if(comparator.compare(obj1, obj2) < 0){
					list.remove(j);
					list.add(j, obj1);
					list.remove(i);
					list.add(i, obj2);
					obj1 = obj2;
				}
			}
		}
	}
	
	public static void insertSort(List list, Comparator comparator, Object obj){
		int size = list.size();
		int minP = 0;
		int maxP = size;
		int middleP = size / 2;
		int iVal = 0;
		
		if(size > 0){
			do{
				if(comparator.compare(list.get(maxP - 1), obj) >= 0){
					list.add(maxP, obj);
					break;
				}else if(comparator.compare(list.get(minP), obj) <= 0){
					list.add(minP, obj);
					break;
				}else{
					iVal = comparator.compare(list.get(middleP), obj);
					
					if(iVal == 0){
						list.add(middleP, obj);
						break;
					}else if(iVal > 0){
						minP = middleP;
						size = maxP - minP;
						middleP = minP + (size / 2);
					}else{
						maxP = middleP;
						size = maxP - minP;
						middleP = minP + (size / 2);
					}
				}
			}while(true);
		}else{
			list.add(obj);
		}
	}
	
}
